﻿using Microsoft.EntityFrameworkCore;
using MinimalistFoodStreet.BackEnd.Infrastructure.Attributes;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response;
using MinimalistFoodStreet.BackEnd.Infrastructure.EFCore;
using MinimalistFoodStreet.BackEnd.Server.IServer.Tables;

namespace MinimalistFoodStreet.BackEnd.Server.Server.Tables
{
    [InjectClass]
    public class HistorysServer : IHistorysServer
    {
        private readonly ServerRepository _serverRepository;

        public HistorysServer(DbContext dbContext)
        {
            _serverRepository = new ServerRepository(dbContext);
        }

        public async Task<ResponseApiDto> Add(RequestCreateHistorysDto dto, int bid)
        {
            var iq = _serverRepository.historysEFCore.Query(d => d.HBusinessId == bid && d.HGoodId == dto.gid);
            int affectedRows = 0;
            if (await iq.AnyAsync())
            {
                // 已经有历史记录了，更新一下访问时间
                History history = await iq.SingleAsync();
                history.HVisitTime = DateTime.Now;
                affectedRows += await _serverRepository.historysEFCore.UpdateAsync(history);
            }
            else
            {
                // 添加历史记录
                History history = new History { HBusinessId = bid, HGoodId = dto.gid, HVisitTime = DateTime.Now };
                affectedRows += await _serverRepository.historysEFCore.AddAsync(history);
            }
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> ClearAll(int bid)
        {
            var iq = _serverRepository.historysEFCore.Query(d => d.HBusinessId == bid);
            int affectedRows = await _serverRepository.historysEFCore.DeleteAsync(iq);
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> Delete(RequestIdHistorysDto dto, int bid)
        {
            var iq = _serverRepository.historysEFCore.Query(d => d.HBusinessId == bid && d.HId == dto.id);
            int affectedRows = await _serverRepository.historysEFCore.DeleteAsync(iq);
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> Pagination(int page, int limit, int bid)
        {
            int total = 0;
            var iq = _serverRepository.historysEFCore.Pagination(d => d.HBusinessId == bid, false, d => d.HVisitTime, out total, page, limit);
            List<ResponseHistorysDto> data = await iq.Select(d => new ResponseHistorysDto
            {
                id = d.HId,
                good = new ResponseGoodList
                {
                    name = d.HGood.GName,
                    id = d.HGood.GId,
                    photo = d.HGood.GPhoto,
                    price = d.HGood.GPrice,
                    promotePrice = d.HGood.Promotions.Where(d => d.PEndTime > DateTime.Now).FirstOrDefault() != null ? d.HGood.Promotions.Where(d => d.PEndTime > DateTime.Now).FirstOrDefault().PDiscountPrice : 0,
                    saleCout = d.HGood.OrderDetails.Where(d => d.OdOrder.OCompleteTime != null).Sum(d => d.OdCount),
                    spec = d.HGood.GSpecs
                }
            }).ToListAsync();
            return ResponseApiDto.Ok("数据已获取", new { total, data });
        }
    }
}
